package com.yss.module;

import com.yss.constant.BusinessEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Description:统一响应结果对象
 * @Author:yss
 * @Date:2024/7/18 21:56
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("项目统一响应结果对象")
public class Result<T> implements Serializable {
    @ApiModelProperty("状态码")
    private Integer code=200;
    @ApiModelProperty("消息")
    private String msg="ok";
    @ApiModelProperty("数据")
    private T data;

    //操作成功
    public static <T> Result<T> success(T data){
        Result result = new Result<>();
        result.setData(data);
        return result;
    }
    //操作失败
    public static <T> Result<T> fail(Integer code,String msg){
        Result result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(null);
        return  result;
    }

    public static <T> Result<T> fail(BusinessEnum bussinessEnum ){
        Result result=new Result();
        result.setCode(bussinessEnum.getCode());
        result.setMsg(bussinessEnum.getDesc());
        result.setData(null);
        return result;
    }

    /**
     * 处理用户的操作
     * @param flag
     * @return
     */
    public static Result<String> handle(Boolean flag) {
        if(flag){
            return Result.success(null);
        }else{
            return Result.fail(BusinessEnum.OPERATION_FAIL);
        }
    }
}
